//
//  GoodServiceInfoView.m
//  B2C
//
//  Created by xiaowu on 2017/8/2.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "GoodServiceInfoView.h"
#import "ServiceInfoCell.h"

static CGFloat ViewHeight = 420 + 49;

@interface GoodServiceInfoView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIView *mainView;

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *button;

@property (nonatomic, strong) NSArray *dataArr;
@end

@implementation GoodServiceInfoView

+ (GoodServiceInfoView *)showGoodServiceInfoViewWithTitles:(NSArray <GoodDetailGtotsModel *> *)titles {
    GoodServiceInfoView *serviceInfoView = [[GoodServiceInfoView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    
    if (!window) {
        window = [[UIApplication sharedApplication].windows lastObject];
    }
    
    [window addSubview:serviceInfoView];
    [window bringSubviewToFront:serviceInfoView];
    [serviceInfoView setDataArr:titles];
    [serviceInfoView bottomToTopAnimation];
    
    return serviceInfoView;
}



- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        _dataArr = [NSMutableArray array];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissSelf)];
        [self.mainView addGestureRecognizer:tapGesture];
        
        [self.tableView registerNib:[UINib nibWithNibName:@"ServiceInfoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    [self addSubview:self.mainView];
    [self addSubview:self.backView];
    
    [self.backView addSubview:self.titleLabel];
    [self.backView addSubview:self.tableView];
    [self.backView addSubview:self.button];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.mainView.frame = [UIScreen mainScreen].bounds;
    self.backView.frame = CGRectMake(0, self.height - ViewHeight, self.width, ViewHeight);
    
    self.titleLabel.frame = CGRectMake(0, 0, self.width, 60);
    self.tableView.frame = CGRectMake(0, self.titleLabel.bottom, self.width, self.backView.height - 60- 49);
    self.button.frame = CGRectMake(0, self.tableView.bottom, self.width, 49);
}

#pragma mark - Method
- (void)removeSelf {
    [self removeFromSuperview];
}

- (void)dismissSelf {
    [self topToBottomAnimation];
}

- (void)bottomToTopAnimation {
    self.backView.frame = CGRectMake(0, self.height, self.width, ViewHeight);
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.backView.frame = CGRectMake(0, weakSelf.height - ViewHeight, weakSelf.width, ViewHeight);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)topToBottomAnimation {
    self.backView.frame = CGRectMake(0, self.height - ViewHeight, self.width, ViewHeight);

    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.backView.frame = CGRectMake(0, weakSelf.height, weakSelf.width, ViewHeight);
    } completion:^(BOOL finished) {
        [self removeSelf];
    }];
}

#pragma mark - Action
- (void)buttonClick:(UIButton *)sender {
    [self dismissSelf];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ServiceInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    GoodDetailGtotsModel *gtotsModel = [self.dataArr safeObjectAtIndex:indexPath.row];
    
    cell.titleLabel.text = gtotsModel.gtname;
    cell.subTitleLabel.text = gtotsModel.gtdescribe;
    return cell;
}


#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}


#pragma mark - Getters
- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [[UIView alloc] init];
    }
    return _mainView;
}

- (UIView *)backView {
    if (!_backView) {
        _backView = [[UIView alloc] init];
    }
    return _backView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView clearExtendCellLine];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _tableView.estimatedRowHeight = 44;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.backgroundColor = [UIColor whiteColor];
        _titleLabel.textColor = [UIColor blackTextColor];
        _titleLabel.font = TextNormalFont16;
        _titleLabel.text = @"服务信息";
    }
    return _titleLabel;
}

- (UIButton *)button {
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_button setBackgroundImage:[UIImage imageWithColor:[UIColor orangePriceColor]] forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _button.titleLabel.font = TextNormalFont14;
        [_button setTitle:@"完成" forState:UIControlStateNormal];
    }
    return _button;
}

@end
